//
//  DKLoginAgreementAlertView.m
//  DanKU
//
//  Created by 吴斌 on 2023/9/24.
//

#import "DKLoginAgreementAlertView.h"
@interface DKLoginAgreementAlertView()<TTTAttributedLabelDelegate>
@property (nonatomic, weak)zhPopupController * dk_LoginAgreementAlertViewPop;
@property (nonatomic,strong) DKLoginAgreementAlertView *dk_LoginAgreementAlertView;
@property (nonatomic, copy)  void(^SureAgreementAlertBlock)(void);
@property (nonatomic, copy) void(^goCheckAgreementBlock)(NSString * url);
@end
@implementation DKLoginAgreementAlertView
-(instancetype)init{
    if(self == [super init]){
        self.backgroundColor = UIColor.whiteColor;
        self.layer.cornerRadius = 50;
        self.clipsToBounds = YES;
        [self dk_addSubView];
    }
    return self;
}
- (void)dk_addSubView{
    UILabel *dk_LoginAgreenHeadLab = [[UILabel alloc] init];
    dk_LoginAgreenHeadLab.textColor = DK_Hex(@"#333333");
    dk_LoginAgreenHeadLab.font = DK_FONT_Semibold(17);
    dk_LoginAgreenHeadLab.text = @"Privacy Agreement";
    [self addSubview:dk_LoginAgreenHeadLab];
    [dk_LoginAgreenHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(24)));
        make.centerX.equalTo(self);
    }];
    //协议
    TTTAttributedLabel * dk_LoginAgreementLab = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
    dk_LoginAgreementLab.numberOfLines = 0;
    dk_LoginAgreementLab.delegate = self;
    [self addSubview:dk_LoginAgreementLab];
    [dk_LoginAgreementLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(29)));
        make.right.equalTo(@(-DK_IsWidthScale(29)));
        make.top.equalTo(dk_LoginAgreenHeadLab.mas_bottom).offset(DK_IsWidthScale(19));
    }];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"Account login is deemed to agree with the《Privacy Agreement》and the 《Registration Agreement》"];
    [attStr addAttribute:NSFontAttributeName value:DK_FONT_Regular(14) range:NSMakeRange(0,attStr.length)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DK_Hex(@"#333333") range:NSMakeRange(0,attStr.length)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DK_Hex(@"#4566FF") range:NSMakeRange(41,19)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DK_Hex(@"#4566FF") range:NSMakeRange(68,attStr.length-68)];
    dk_LoginAgreementLab.delegate = self;
    dk_LoginAgreementLab.attributedText = attStr;
    dk_LoginAgreementLab.enabledTextCheckingTypes = NSTextCheckingTypeLink;
    dk_LoginAgreementLab.linkAttributes = @{NSForegroundColorAttributeName:DK_Hex(@"#4566FF")};
    [dk_LoginAgreementLab addLinkToURL:[NSURL URLWithString:DK_PrivacyAgreement] withRange:NSMakeRange(41,19)];
    [dk_LoginAgreementLab addLinkToURL:[NSURL URLWithString:DK_RegisterAgreement] withRange:NSMakeRange(68,attStr.length-68)];
    //next
    UIButton * dk_LoginNextBtn = [[UIButton alloc] init];
    [dk_LoginNextBtn setTitle:@"Next" forState:UIControlStateNormal];
    [dk_LoginNextBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
    dk_LoginNextBtn.titleLabel.font = DK_FONT_Semibold(16);
    [dk_LoginNextBtn setBackgroundColor:DK_Hex(@"#4566FF")];
    dk_LoginNextBtn.layer.cornerRadius = DK_IsWidthScale(25);
    dk_LoginNextBtn.clipsToBounds = YES;
    [dk_LoginNextBtn addTarget:self action:@selector(sureAgreement) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:dk_LoginNextBtn];
    [dk_LoginNextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.bottom.equalTo(@(-DK_IsWidthScale(40)));
    }];
    //close
    UIButton * dk_LoginCloseBtn = [[UIButton alloc] init];
    [dk_LoginCloseBtn setBackgroundImage:DK_GetImage(@"dk_LoginAgreementClose") forState:UIControlStateNormal];
    [dk_LoginCloseBtn addTarget:self action:@selector(goCancle) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:dk_LoginCloseBtn];
    [dk_LoginCloseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(20)));
        make.right.equalTo(@(-DK_IsWidthScale(20)));
        make.height.equalTo(@(DK_IsWidthScale(17)));
        make.width.equalTo(@(DK_IsWidthScale(17)));
    }];
}
- (void)sureAgreement{
    if(self.SureAgreementAlertBlock){
        self.SureAgreementAlertBlock();
        [_dk_LoginAgreementAlertViewPop dismiss];
    }
}
- (void)goCancle{
    if(self.SureAgreementAlertBlock){
        self.SureAgreementAlertBlock();
        [_dk_LoginAgreementAlertViewPop dismiss];
    }
}

+ (void)dk_ShowLoginAgreementAlertViewNext:(UIView *)keyView Complete:(void(^)(void))complete WithGoUrl:(void(^)(NSString * url))CheckAgreementBlock{
    DKLoginAgreementAlertView * dk_LoginAgreementAlertView = [[DKLoginAgreementAlertView alloc] init];
    zhPopupController * sureAgreemnetCtrl = [[zhPopupController alloc] initWithView:dk_LoginAgreementAlertView size:CGSizeMake(DK_SCREEN_W - DK_IsWidthScale(60), DK_IsWidthScale(268))];
    dk_LoginAgreementAlertView.SureAgreementAlertBlock = complete;
    dk_LoginAgreementAlertView.goCheckAgreementBlock = CheckAgreementBlock;
    sureAgreemnetCtrl.presentationStyle = zhPopupSlideStyleTransform;
    sureAgreemnetCtrl.layoutType = zhPopupLayoutTypeCenter;
    sureAgreemnetCtrl.presentationTransformScale = 0.56;
    sureAgreemnetCtrl.dismissonTransformScale = 0.56;
    sureAgreemnetCtrl.dismissOnMaskTouched = NO;
    dk_LoginAgreementAlertView.dk_LoginAgreementAlertViewPop = sureAgreemnetCtrl;
    [sureAgreemnetCtrl showInView:keyView completion:^{
    }];
}
#pragma mark -- TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url{
    if(self.goCheckAgreementBlock){
        self.goCheckAgreementBlock(url.absoluteString);
    }
}
@end
